package com.qdlc.p2p.biz.tpp;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

import com.qdlc.p2p.biz.service.BorrowAutoService;
import com.qdlc.p2p.biz.tpp.model.SinaBindingVerify;
import com.qdlc.p2p.biz.tpp.model.SinaCollectTrade;
import com.qdlc.p2p.biz.tpp.model.SinaHelper;
import com.qdlc.p2p.biz.tpp.model.SinaModel;
import com.qdlc.p2p.biz.tpp.model.SinaQueryAccountDetails;
import com.qdlc.p2p.biz.tpp.model.SinaQueryBalance;
import com.qdlc.p2p.biz.tpp.model.SinaQueryFundYield;
import com.qdlc.p2p.biz.tpp.model.SinaQueryWebBalance;
import com.qdlc.p2p.biz.tpp.model.SinaRecharge;
import com.qdlc.p2p.biz.tpp.model.SinaWithdraw;
import com.qdlc.p2p.common.constant.SinaConstant;
import com.qdlc.p2p.common.exception.BorrowException;
import com.qdlc.p2p.common.exception.MemberAgentException;
import com.qdlc.p2p.common.exception.UserException;
import com.qdlc.p2p.common.util.BeanUtil;
import com.qdlc.p2p.common.util.BigDecimalUtil;
import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.common.util.NumberUtil;
import com.qdlc.p2p.common.util.OrderNoUtil;
import com.qdlc.p2p.dal.dao.AccountBankDao;
import com.qdlc.p2p.dal.dto.AccountBank;
import com.qdlc.p2p.dal.dto.BorrowAuto;
import com.qdlc.p2p.dal.dto.MemberInfos;
import com.qdlc.p2p.dal.dto.User;
import com.qdlc.p2p.dal.dto.UserIdentify;
import com.qdlc.p2p.dal.model.TppAccountModel;
import com.qdlc.p2p.dal.model.TppModel;

/**
 * 
 * 新浪支付 service
 * 
 * @author zlhx
 * @version 2.0
 * @date 2015年8月10日 下午2:55:51
 */
public class TppSinaServiceImpl extends TppBaseImpl implements TppBaseInterface {
	private static final Logger logger = Logger.getLogger(TppSinaServiceImpl.class);

	@Override
	public Object tppRegister(TppModel model) {
		return SinaHelper.tppRegister(model);
	}

	@Override
	public Object tppRealName(TppModel model) {
		SinaModel sModel = SinaHelper.tppRealName(model);
		if (sModel == null) {
			logger.info("新浪实名失败");
			throw new UserException("新浪实名失败！");
		}
		return sModel;
	}

	@Override
	public Object bindingVerify(TppModel model, UserIdentify userIdentify, MemberInfos memberInfo) {
		SinaBindingVerify bv = (SinaBindingVerify) SinaHelper.bindingVerify(model, userIdentify);
		if (bv == null) {
			logger.error("新浪绑定认证信息失败");
			throw new UserException("新浪绑定认证信息失败！");
		}
		return bv;
	}

	@Override
	public Object tppRecharge(TppModel model) {
		// 网银充值
		SinaRecharge recharge = SinaHelper.tppRecharge(model);
		model.setOrderId(recharge.getOut_trade_no());
		return recharge;
	}

	@Override
	public Object tppQueryBalance(TppModel model) {
		SinaQueryBalance balance = (SinaQueryBalance) SinaHelper.tppQueryBalance(model);
		TppAccountModel tppAccountModel = new TppAccountModel(balance.getBalance(), balance.getAvailable_balance(),
				BigDecimalUtil.sub(NumberUtil.getDouble(balance.getBalance()),
						NumberUtil.getDouble(balance.getAvailable_balance())) + "",
				balance.getBonus(), "");
		return tppAccountModel;
	}

	@Override
	public Object tppQueryWebBalance() {
		SinaQueryWebBalance balance = (SinaQueryWebBalance) SinaHelper.tppQueryWebBalance();
		TppAccountModel tppAccountModel = new TppAccountModel(balance.getBalance(), balance.getAvailable_balance(),
				BigDecimalUtil.sub(NumberUtil.getDouble(balance.getBalance()),
						NumberUtil.getDouble(balance.getAvailable_balance())) + "",
				balance.getBonus(), "");
		return tppAccountModel;
	}

	@Override
	public Object tppQueryAccountDetails(TppModel model) {
		SinaQueryAccountDetails ad = (SinaQueryAccountDetails) SinaHelper.tppQueryAccountDetails(model);
		TppAccountModel tppAccountModel = new TppAccountModel(ad.getDetail_list(), ad.getTotal_income(), ad.getTotal_payout());
		return tppAccountModel;
	}
	
	@Override
	public Object tppQueryAccountDetailsPagination(TppModel model) {
		//增加入参，返回分页信息
		SinaQueryAccountDetails ad = (SinaQueryAccountDetails) SinaHelper.tppQueryAccountDetailsPagination(model);
		if(ad!=null){
		TppAccountModel tppAccountModel = new TppAccountModel(ad.getDetail_list(),ad.getPage_size(),ad.getTotal_item(),ad.getPage_no());
		return tppAccountModel;
		}
		else{
			return null;
		}
	}
	
	@Override
	public Object tppQueryFundYield(TppModel model) {
		SinaQueryFundYield queryFundYield = (SinaQueryFundYield) SinaHelper.tppQueryFundYield(model);
		TppAccountModel tppAccountModel = new TppAccountModel("", "", "", "", queryFundYield.getYield_list());
		return tppAccountModel;
	}

	@Override
	public Object tppFreeze(TppModel model) {
		// 封装register对象
		return SinaHelper.tppFreeze(model);
	}

	@Override
	public Object tppUnFreeze(TppModel model) {
		return SinaHelper.tppUnFreeze(model);
	}

	@Override
	public Object tppCollectTrade(TppModel model) {
		SinaModel sModel = SinaHelper.tppCollectTrade(model);
		if (sModel == null) {
			throw new BorrowException("代收失败!");
		}
		return sModel;
	}

	@Override
	public Object tppCash(TppModel model) {
		AccountBankDao accountBankDao = (AccountBankDao) BeanUtil.getBean("accountBankDao");
		AccountBank accountBank = accountBankDao.find(model.getUser().getUserId(), model.getBankCardId());
		if (accountBank == null) {
			logger.error("银行卡不存在");
			throw new UserException("银行卡不存在！");
		}
		model.setBank_card_id(accountBank.getTppCardId());
		SinaWithdraw withdraw = (SinaWithdraw) SinaHelper.tppCash(model);
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("apiCash", withdraw);
		data.put("resultType", 1);
		return data;
	}

	@Override
	public Object tppPayTrade(TppModel model) {
		logger.info("代付开始...");
		return SinaHelper.tppPayTrade(model);
	}

	@Override
	public Object tppTender(TppModel model) {
		SinaCollectTrade collTrade = (SinaCollectTrade) SinaHelper.tppCollectTrade(model);
		return collTrade;
	}

	@Override
	public Object tppAddTender(TppModel model) {
		model.setOrderId(OrderNoUtil.getInstance().getSerialNumber());
		Map<String, Object> map = new HashMap<String, Object>();
		if (SinaConstant.PAY_METHOD_ONLINE_BANK.equals(model.getPay_method())) {

		} else {
			map.put("tender", null);
			map.put("resultType", 1);
		}
		return map;
	}

	@Override
	public Object tppAutoTender(TppModel model) {

		return tppCollectTrade(model);
	}

	@Override
	public Object tppLoan(TppModel model) {
		return tppPayTrade(model);
	}

	@Override
	public Object tppRepayment(TppModel model) {
		return tppPayTrade(model);
	}

	@Override
	public Object tppAddBank(TppModel model) {
		return SinaHelper.tppBindBankCard(model);
	}

	@Override
	public Object tppDeleteBank(TppModel model) {
		User user = model.getUser();
		AccountBankDao accountBankDao = (AccountBankDao) BeanUtil.getBean("accountBankDao");
		AccountBank accountBank = accountBankDao.find(user.getUserId(), model.getBankCardId());
		model.setBank_card_id(accountBank.getTppCardId());
		return SinaHelper.tppUnBindBankCard(model);
	}

	@Override
	public Object tppQueryBankCard(TppModel model) {
		User user = model.getUser();
		model.setUser(user);
		return SinaHelper.tppQueryBankCard(model);
	}

	@Override
	public Object tppAutoTenderPlan(TppModel model) {
		BorrowAutoService borrowAutoService = (BorrowAutoService) BeanUtil.getBean("borrowAutoService");
		BorrowAuto ba = borrowAutoService.findByUser(model.getUser());
		ba.setEnable(true);
		ba.setUpdateTime(DateUtil.getNow());
		try {
			borrowAutoService.update(ba);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ba;
	}

	@Override
	public boolean tppCloseAutoTenderPlan(TppModel model) {
		try {
            BorrowAutoService borrowAutoService = (BorrowAutoService) BeanUtil.getBean("borrowAutoService");
            BorrowAuto ba = borrowAutoService.findByUser(model.getUser());
            ba.setEnable(false);
			borrowAutoService.update(ba);
		} catch (Exception e) {
			e.printStackTrace();
            return false;
		}
		return true;
	}

	@Override
	public Object tppRefund(TppModel model) {
		logger.info("退款开始 ... ");
		return SinaHelper.tppRefund(model);
	}

	@Override
	public Object tppMemberAgent(TppModel agentModel, MemberInfos memberInfo) {
		SinaModel sModel = SinaHelper.tppMemberAgent(agentModel);
		if (sModel == null) {
			logger.info("经办人信息认证失败");
			throw new MemberAgentException("经办人信息认证失败", 1);
		}
		return sModel;
	}

}
